<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加轮播图</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="菜品id" prop="dishesId">
        <el-input v-model="ruleForm.dishesId"></el-input>
      </el-form-item>
      <el-form-item label="图片路径" prop="url">
        <el-input v-model="ruleForm.url"></el-input>
      </el-form-item>
      <el-form-item label="图片简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="图片宽度" prop="width">
        <el-input v-model="ruleForm.width"></el-input>
      </el-form-item>
      <el-form-item label="图片高度" prop="height">
        <el-input v-model="ruleForm.height"></el-input>
      </el-form-item>
      <el-form-item label="是否为封面图片" prop="isCover">
        <el-switch
            v-model="ruleForm.isCover"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#cccccc">
        </el-switch>
      </el-form-item>
      <el-form-item label="排序序号" prop="sort">
        <el-input v-model="ruleForm.sort"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      ruleForm: {
        dishesId: '1',
        url: 'https://img2.baidu.com/it/u=2764146643,2653542663&fm=253&fmt=auto&app=138&f=JPEG?w=667&h=500.jpg',
        description: '测试品牌的简介001',
        width: '11',
        height:'11',
        isCover: 1,
        sort: 99
      },
      rules: {
        dishesId: [
          {required: true, message: '请输入菜品id', trigger: 'blur'},
          {min: 1, max: 15, message: '长度在 1到 15 个字符', trigger: 'blur'}
        ],
        url: [
          {required: true, message: '请输入图片路径', trigger: 'blur'},
          {min: 30, max: 256, message: '长度在 30 到 256 个字符', trigger: 'blur'}
        ],
        description: [
          {required: true, message: '请输入图片简介', trigger: 'blur'},
          {min: 5, max: 55, message: '长度在 5 到 55 个字符', trigger: 'blur'}
        ],
        width: [
          {required: true, message: '请输入图片宽度', trigger: 'blur'},
          {min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur'}
        ],
        height: [
          {required: true, message: '请输入图片高度', trigger: 'blur'},
          {min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur'}
        ],
        sort: [
          {required: true, message: '请输入品牌排序序号', trigger: 'blur'},
          {pattern: /^(\d{1}|[1-9]{1}[0-9]?)$/, message: '排序序号必须是0~99之间的数字', trigger: 'blur'}
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9989/pictures/add-new';
          console.log('url = ' + url);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('formData = ' + formData);

          this.axios
              .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
              .post(url, formData).then((response) => {
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              this.$message({
                message: '添加轮播图成功！',
                type: 'success'
              });
              this.resetForm(formName);
            } else {
              this.$message.error(responseBody.message);
            }
          });
        } else {
          alert('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style scoped>
</style>